﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FbKPI.Facade;
using FbKPI.Model;
using FbKPI.Facade.Validator;
using System.Transactions;


namespace FBKPI.Pages
{
    public partial class InputBreakDownKPIPage : System.Web.UI.Page
    {
        protected override void OnPreRender(EventArgs e)
        {
           // ViewState["update"] = Session["update"];
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //Session["update"] = Server.UrlEncode(System.DateTime.Now.ToString());

                int ID = Convert.ToInt32(Request.QueryString["KPI"]);

                if (ID != 0)
                {
                    KPIFacade df = new KPIFacade();
                    KPI KPIs = df.GetKPIByID(ID);

                    if (KPIs != null)
                    {
                        hfKpiId.Value = ID.ToString();
                        txt_kpidescription.Text = KPIs.Description;
                        chk_active.Checked = KPIs.isActive;
                        chk_weightenableflag.Checked = KPIs.WeightEnableFlag;
                        chk_uomenableflag.Checked = KPIs.UoMEnableFlag;
                        txt_kpigroup.Text = KPIs.GroupKPI == null ? string.Empty : KPIs.GroupKPI.Description;
                        txt_kpicategory.Text = KPIs.CategoryKPI == null ? string.Empty : KPIs.CategoryKPI.Description;
                        txt_kpitype.Text = KPIs.TypeKPI == null ? string.Empty : KPIs.TypeKPI.Description;
                        txt_kpisubtype.Text = KPIs.SubTypeKPI == null ? string.Empty : KPIs.SubTypeKPI.Description;
                        txt_kpiuom.Text = KPIs.UoM == null ? string.Empty : KPIs.UoM.Description;
                        txt_kpiuommax.Text = KPIs.UoMDetailMax == null ? string.Empty : KPIs.UoMDetailMax.Description + " || " + KPIs.UoMDetailMax.ListValue;
                        txt_kpiuommin.Text = KPIs.UoMDetailMin == null ? string.Empty : KPIs.UoMDetailMin.Description + " || " + KPIs.UoMDetailMin.ListValue;
                        txt_kpiyear.Text = KPIs.Year.ToString();
                        txt_weight.Text = KPIs.Weight.ToString();
                        txt_kpiperiode.Text = KPIs.Periode;
                        txt_polarization.Text = KPIs.Polarization;
                        txt_uomformat.Value = KPIs.UoM.Format.ToString();


                        txt_childgroup.Text = KPIs.GroupKPI == null ? string.Empty : KPIs.GroupKPI.Description;
                        txt_childgroupid.Value = KPIs.GroupKPI == null ? string.Empty : KPIs.GroupKPI.ID.ToString();
                        txt_childcategory.Text = KPIs.CategoryKPI == null ? string.Empty : KPIs.CategoryKPI.Description;
                        txt_childcategoryid.Value = KPIs.CategoryKPI == null ? string.Empty : KPIs.CategoryKPI.ID.ToString();
                        txt_childtype.Text = KPIs.TypeKPI == null ? string.Empty : KPIs.TypeKPI.Description;
                        txt_childtypeid.Value = KPIs.TypeKPI == null ? string.Empty : KPIs.TypeKPI.ID.ToString();
                        txt_childsubtype.Text = KPIs.SubTypeKPI == null ? string.Empty : KPIs.SubTypeKPI.Description;
                        txt_childsubtypeid.Value = KPIs.SubTypeKPI == null ? string.Empty : KPIs.SubTypeKPI.ID.ToString();
                        txt_childyear.Text = KPIs.Year.ToString();
                        txt_childperiode.Text = KPIs.Periode;
                        txt_childpolarization.Text = KPIs.Polarization;
                        txt_childuom.Text = KPIs.UoM == null ? string.Empty : KPIs.UoM.Description;
                        txt_childuomid.Value = KPIs.UoM == null ? string.Empty : KPIs.UoM.ID.ToString();
                        txt_childuommax.Text = KPIs.UoMDetailMax == null ? string.Empty : KPIs.UoMDetailMax.Description;
                        txt_childuommaxid.Value = KPIs.UoMDetailMax == null ? string.Empty : KPIs.UoMDetailMax.ID.ToString();
                        txt_childuommin.Text = KPIs.UoMDetailMin == null ? string.Empty : KPIs.UoMDetailMin.Description;
                        txt_childuomminid.Value = KPIs.UoMDetailMin == null ? string.Empty : KPIs.UoMDetailMin.ID.ToString();
                        txt_childweight.Text = KPIs.Weight.ToString();

                        txt_uomenableflag.Value = KPIs.UoMEnableFlag.ToString();
                        txt_weightenableflag.Value = KPIs.WeightEnableFlag.ToString();
                    }
                }
            }
        }
        protected void SaveAction(object sender, EventArgs e)
        {
            //if (Session["update"].ToString() == ViewState["update"].ToString())
            //{
            //    int KPIID = Convert.ToInt32(Request.QueryString["KPI"]);
            //    string msg = "OK";
            //    int ID = Convert.ToInt16(txt_childkpiid.Text);
            //    string Description = txt_childdescription.Text ;
            //    bool isActive = chk_childisactive.Checked;
            //    bool weightEnable = chk_childweightenableflag.Checked;
            //    bool uomEnable = chk_childuomenableflag.Checked;
            //    int GroupKPIID = Convert.ToInt16(txt_childgroupid.Value);
            //    int CategoryKPIID = Convert.ToInt16(txt_childcategoryid.Value);
            //    int TypeKPIID = Convert.ToInt16(txt_childtypeid.Value);
            //    int SubTypeKPIID = Convert.ToInt16(txt_childsubtypeid.Value);
            //    int UOMID = Convert.ToInt16(txt_childuomid.Value == string.Empty ? "0" : txt_childuomid.Value);
            //    int UOMMaxID = Convert.ToInt16(txt_childuommaxid.Value == string.Empty ? "0" : txt_childuommaxid.Value);
            //    int UOMMinID = Convert.ToInt16(txt_childuomminid.Value == string.Empty ? "0" : txt_childuomminid.Value);
            //    int KPIParentID = KPIID;
            //    int KPIReplacementID = Convert.ToInt16(txt_childkpireplacementid.Value == string.Empty ? "0" : txt_childkpireplacementid.Value);
            //    int Year = Convert.ToInt16(txt_childyear.Text);
            //    int Weight = Convert.ToInt16(txt_childweight.Text == string.Empty ? "0" : txt_childweight.Text);
            //    string Periode = txt_childperiode.Text;
            //    string Polarization = txt_childpolarization.Text;
            //    string TargetDescription = txt_childtargetdescription.Text;
            //    int ManualCalculation = Convert.ToInt16(txt_childmanualcalculation.Text == string.Empty ? "0" : txt_childmanualcalculation.Text);
            //    string CalculationType = txt_childmanualcalculation.Text;
            //    DateTime? ReplacementDate = txt_childreplacementdate.Text == string.Empty ? (DateTime?)null : Convert.ToDateTime(txt_childreplacementdate.Text);
            //    string ReplacementDescription = txt_childreplacementdesc.Text;
            //    string DetailInformation = txt_childdetailinformation.Text;
            //    string StrategicObjective = txt_childstrategicobjective.Text;
            //    Employee CurrUser = new EmployeeFacade().GetEmployeeByID(Convert.ToInt32(HttpContext.Current.Request.QueryString["User"]));

            //    KPIFacade df = new KPIFacade();
            //    KPI KPIs = df.GetKPIByID(ID);
            //    KPIValidator dv = new KPIValidator();

            //    if (KPIs == null)
            //    {
            //        KPIs = new KPI();
            //        KPIs.ID = ID;
            //        KPIs.Description = Description;
            //        KPIs.isActive = isActive;
            //        KPIs.WeightEnableFlag = weightEnable;
            //        KPIs.UoMEnableFlag = uomEnable;
            //        KPIs.GroupKPI = new GroupKPIFacade().GetGroupKPIByID(GroupKPIID);
            //        KPIs.CategoryKPI = new CategoryKPIFacade().GetCategoryKPIByID(CategoryKPIID);
            //        KPIs.TypeKPI = new TypeKPIFacade().GetTypeKPIByID(TypeKPIID);
            //        KPIs.SubTypeKPI = new SubTypeKPIFacade().GetSubTypeKPIByID(SubTypeKPIID);
            //        KPIs.UoM = new UoMFacade().GetUoMByID(UOMID);
            //        KPIs.UoMDetailMax = new UoMDetailFacade().GetUoMDetailByID(UOMMaxID);
            //        KPIs.UoMDetailMin = new UoMDetailFacade().GetUoMDetailByID(UOMMinID);
            //        KPIs.KPIParent = new KPIFacade().GetKPIByID(KPIParentID);
            //        KPIs.KPIReplacement = new KPIFacade().GetKPIByID(KPIReplacementID);
            //        KPIs.Year = Year;
            //        KPIs.Weight = Weight;
            //        KPIs.Periode = Periode;
            //        KPIs.Polarization = Polarization;
            //        KPIs.TargetDescription = TargetDescription;
            //        KPIs.ManualCalculation = ManualCalculation;
            //        KPIs.KPICalculationType = CalculationType;
            //        KPIs.ReplacementDate = ReplacementDate;
            //        KPIs.ReplacementDescription = ReplacementDescription;
            //        KPIs.KPIDetailInformation = DetailInformation;
            //        KPIs.KPIStrategicObjective = StrategicObjective;
            //        KPIs.CreateBy = CurrUser.Email;
            //        KPIs.BusinessGroupID = CurrUser.BusinessGroupID;
            //        KPIs.EmployeeIDVerifier = CurrUser.ID;
            //        KPIs.isLockWeight = true;
            //        KPIs.PercentageMax = 100;
            //        KPIs.PercentageMin = 80;
            //        KPIs.AssignWeight = "";
            //        msg = dv.ValidateOnSave(KPIs);
            //    }
            
            //    if (msg == "OK")
            //    {
            //        using (TransactionScope trans = new TransactionScope(TransactionScopeOption.RequiresNew))
            //        {
            //            try
            //            {

            //                string FlagInsertKPIUser = "User";
            //                //msg = df.Save(KPIs,FlagInsertKPIUser);

                           
            //                KPIAssign ka = new KPIAssign()
            //                {
            //                    ID = 0,
            //                    isActive = true,
            //                    KPI = KPIs,
            //                    Location = CurrUser.SubBranch.Location,
            //                    ManualCalculation = KPIs.ManualCalculation,
            //                    Polarization = KPIs.Polarization,
            //                    RGM = CurrUser.SubBranch.Location.RGM,
            //                    SubBranch = CurrUser.SubBranch,
            //                    UoM = KPIs.UoM,
            //                    UoMDetailMax = KPIs.UoMDetailMax,
            //                    UoMDetailMin = KPIs.UoMDetailMin,
            //                    UoMEnableFlag = KPIs.UoMEnableFlag,
            //                    Verifier = CurrUser,
            //                    Weight = KPIs.Weight,
            //                    WeightEnableFlag = KPIs.WeightEnableFlag,
            //                    CreateBy = CurrUser.Email
            //                };

            //                KPIAssignDetail kad = new KPIAssignDetail()
            //                {
            //                    ID = 0,
            //                    CreateBy = CurrUser.Email,
            //                    Employee = CurrUser,
            //                    isActive = true,
            //                    KPI = KPIs,
            //                    KPIAssign = ka,
            //                    ManualCalculation = KPIs.ManualCalculation,
            //                    Polarization = KPIs.Polarization,
            //                    UoM = KPIs.UoM,
            //                    UoMDetailMax = KPIs.UoMDetailMax,
            //                    UoMDetailMin = KPIs.UoMDetailMin,
            //                    UoMEnableFlag = KPIs.UoMEnableFlag,
            //                    Weight = KPIs.Weight,
            //                    WeightEnableFlag = KPIs.WeightEnableFlag,
            //                    Mandatory = true,// ddlMandatory.SelectedValue == "1" ? true : false,
            //                    AssignBy = CurrUser.ID

            //                };


            //                if (df.SaveKPIBreakDown(KPIs, FlagInsertKPIUser, ka, kad))
            //                    msg = "Transaction Succes";
            //                else
            //                    msg = "Transaction Error";
            //                Response.Write("<script>alert('" + msg + "');</script>");
            //            }
            //            catch(Exception ex)
            //            {
            //                Tools.LogWriter.LogWriter logwriter = new Tools.LogWriter.LogWriter();
            //                logwriter.WriteLog(msg, ex.StackTrace.ToString());
            //                Response.Write("<script>alert('" + msg + "');</script>");
            //            }
            //            finally
            //            {
            //                trans.Complete();
            //                trans.Dispose();
            //            }
            //        }
            //    }
            //    else
            //    {
            //        Response.Write("<script>alert('" + msg + "');</script>");
            //    }
            //    Session["update"] = Server.UrlEncode(System.DateTime.Now.ToString());
            //}
        }
    }
}